package ch6;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserDao userDao1= new UserDao();
UserBean userBean = (UserBean)request.getSession ().getAttribute ("user");
Integer createdBy = userBean.getId ();
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd");
        Date birthday = null;

            try {
                birthday=sdf.parse (request.getParameter ("birthday"));
            } catch (ParseException e) {
               throw new RuntimeException (e);
            }
UserBean tmp = new UserBean ();
            Date creationDate = new Date ();
            tmp.setUserName (request.getParameter ("userName"));

        tmp.setUserPassword (request.getParameter ("userPassword"));
        tmp.setUserCode (request.getParameter ("userPassword"));
        tmp.setUserPassword (request.getParameter ("userCode"));
        tmp.setGender (Integer.parseInt (request.getParameter ("gender")));
        tmp.setUserRole (Integer.parseInt (request.getParameter ("userRole")));
        tmp.setPhone (request.getParameter ("phone"));
        tmp.setAddress (request.getParameter ("address"));
        tmp.setBirthday (birthday);
        tmp.setCreationDate (creationDate);
tmp.setCreatedBy (createdBy);
Integer result = userDao1.findAll ();
if (result > 0){
    response.sendRedirect ("login.jsp");
}else {
    request.setAttribute ("message","注册失败");
    request.getRequestDispatcher ("/register.jsp").forward (request,response);
}

    }
}
